/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab1;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.UUID;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Кирилл
 */
public final class EditWindow extends javax.swing.JDialog {

    /**
     * Creates new form AddWindow
     */
    private Task editedTask;
    private static boolean isAdd;
    private MaskFormatter formatter;

    public EditWindow(java.awt.Frame parent, boolean modal, boolean isAdd) {
        super(parent, modal);

        initComponents();
        try {
            formatter = new MaskFormatter("##");
            //formatter.setValidCharacters("0123456789");
            taskStartHour.setFormatterFactory(new DefaultFormatterFactory(formatter));
            taskEndHour.setFormatterFactory(new DefaultFormatterFactory(formatter));
            taskStartMinute.setFormatterFactory(new DefaultFormatterFactory(formatter));
            taskEndMinute.setFormatterFactory(new DefaultFormatterFactory(formatter));
            alertTimeHour.setFormatterFactory(new DefaultFormatterFactory(formatter));
            alertTimeMinute.setFormatterFactory(new DefaultFormatterFactory(formatter));
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
        }
        EditWindow.isAdd = isAdd;
        if (isAdd) {
            recieveData(null);
            editedTask.getStime().set(Calendar.SECOND, 0);
            editedTask.getFtime().set(Calendar.SECOND, 0);
            editedTask.getWtime().set(Calendar.SECOND, 0);
            editedTask.getStime().set(Calendar.MILLISECOND, 0);
            editedTask.getFtime().set(Calendar.MILLISECOND, 0);
            editedTask.getWtime().set(Calendar.MILLISECOND, 0);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        approveButton = new javax.swing.JButton();
        taskName = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taskDescription = new javax.swing.JTextArea();
        taskStartDay = new javax.swing.JComboBox();
        taskStartMonth = new javax.swing.JComboBox();
        taskStartYear = new javax.swing.JComboBox();
        taskContacts = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        taskEndDay = new javax.swing.JComboBox();
        taskEndMonth = new javax.swing.JComboBox();
        taskEndYear = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        alertTimeDay = new javax.swing.JComboBox();
        alertTimeMonth = new javax.swing.JComboBox();
        alertTimeYear = new javax.swing.JComboBox();
        taskStartHour = new javax.swing.JFormattedTextField();
        taskStartMinute = new javax.swing.JFormattedTextField();
        taskEndHour = new javax.swing.JFormattedTextField();
        taskEndMinute = new javax.swing.JFormattedTextField();
        alertTimeHour = new javax.swing.JFormattedTextField();
        alertTimeMinute = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        approveButton.setText("Save");
        approveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                approveButtonActionPerformed(evt);
            }
        });

        taskName.setText("New Task");
        taskName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taskNameKeyReleased(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        taskDescription.setColumns(20);
        taskDescription.setRows(5);
        taskDescription.setText("Description\n");
        taskDescription.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taskDescriptionKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(taskDescription);

        taskStartDay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                taskStartDayMouseClicked(evt);
            }
        });

        taskStartMonth.setModel(new DefaultComboBoxModel(Helper.getMonths()));
        taskStartMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskStartMonthActionPerformed(evt);
            }
        });

        taskStartYear.setModel(new DefaultComboBoxModel(Helper.getYears(2012, 2015)));
        taskStartYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskStartYearActionPerformed(evt);
            }
        });

        taskContacts.setText("Contacts");
        taskContacts.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taskContactsKeyReleased(evt);
            }
        });

        jLabel1.setText(":");

        taskEndMonth.setModel(new DefaultComboBoxModel(Helper.getMonths()));
        taskEndMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskEndMonthActionPerformed(evt);
            }
        });

        taskEndYear.setModel(new DefaultComboBoxModel(Helper.getYears(2012, 2015)));
        taskEndYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskEndYearActionPerformed(evt);
            }
        });

        jLabel2.setText("Show alert at:");

        alertTimeMonth.setModel(new DefaultComboBoxModel(Helper.getMonths()));
        alertTimeMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alertTimeMonthActionPerformed(evt);
            }
        });

        alertTimeYear.setModel(new DefaultComboBoxModel(Helper.getYears(2012, 2015)));
        alertTimeYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alertTimeYearActionPerformed(evt);
            }
        });

        taskStartHour.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#, ##"))));
        taskStartHour.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                taskStartHourFocusLost(evt);
            }
        });

        taskStartMinute.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                taskStartMinuteFocusLost(evt);
            }
        });

        taskEndHour.setText("jFormattedTextField3");
        taskEndHour.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                taskEndHourFocusLost(evt);
            }
        });

        taskEndMinute.setText("jFormattedTextField4");
        taskEndMinute.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                taskEndMinuteFocusLost(evt);
            }
        });

        alertTimeHour.setText("jFormattedTextField5");
        alertTimeHour.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                alertTimeHourFocusLost(evt);
            }
        });

        alertTimeMinute.setText("jFormattedTextField6");
        alertTimeMinute.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                alertTimeMinuteFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(alertTimeYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelButton)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(taskName, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(55, 55, 55)
                        .addComponent(alertTimeHour, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(alertTimeMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))
                    .addComponent(taskContacts, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(taskStartDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(taskEndDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(taskStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(taskEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(taskStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(taskStartMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(taskEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(4, 4, 4)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(taskStartYear, 0, 43, Short.MAX_VALUE)
                                            .addComponent(taskEndYear, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(taskEndMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))))
                            .addComponent(approveButton, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(alertTimeDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(alertTimeMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(2, 2, 2)))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(taskName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(taskStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskStartDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(taskContacts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(alertTimeHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alertTimeMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alertTimeDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alertTimeMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alertTimeYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(approveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void approveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_approveButtonActionPerformed
        // TODO add your handling code here:
        editedTask.getStime().set(Integer.parseInt((String) taskStartYear.getSelectedItem()),
                taskStartMonth.getSelectedIndex(),
                Integer.parseInt((String) taskStartDay.getSelectedItem()),
                Integer.parseInt(taskStartHour.getText()),
                Integer.parseInt(taskStartMinute.getText()));
        editedTask.getFtime().set(Integer.parseInt((String) taskEndYear.getSelectedItem()),
                taskEndMonth.getSelectedIndex(),
                Integer.parseInt((String) taskEndDay.getSelectedItem()),
                Integer.parseInt(taskEndHour.getText()),
                Integer.parseInt(taskEndMinute.getText()));
        editedTask.getWtime().set(Integer.parseInt((String) alertTimeYear.getSelectedItem()),
                alertTimeMonth.getSelectedIndex(),
                Integer.parseInt((String) alertTimeDay.getSelectedItem()),
                Integer.parseInt(alertTimeHour.getText()),
                Integer.parseInt(alertTimeMinute.getText()));
        Calendar c = Calendar.getInstance();
        if (editedTask.getStime().compareTo(c) < 0 || editedTask.getFtime().compareTo(c) < 0
                || editedTask.getWtime().compareTo(c) < 0) {
            JOptionPane.showMessageDialog(this, "Some of dates is lesser than current");
        } else if (editedTask.getWtime().compareTo(editedTask.getStime()) > 0) {
            JOptionPane.showMessageDialog(this, "Alert Time is bigger than start time");
        } else if (editedTask.getStime().compareTo(editedTask.getFtime()) > 0) {
            JOptionPane.showMessageDialog(this, "Finish time is before start time");
        } else {
            if (isAdd) {
                MainWindow.addTask(editedTask);
            } else {
                MainWindow.changeTask(editedTask);
            }
            this.setVisible(false);
        }
    }//GEN-LAST:event_approveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void taskStartMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskStartMonthActionPerformed
        // TODO add your handling code here:
        int fixedDay = taskStartDay.getSelectedIndex();
        String month = (String) taskStartMonth.getSelectedItem();
        int year = Integer.parseInt(taskStartYear.getSelectedItem().toString());
        boolean isLeap = ("February".equals(month) && new GregorianCalendar().isLeapYear(year)) ? true : false;
        taskStartDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, isLeap)));
        try {
            taskStartDay.setSelectedIndex(fixedDay);
        } catch (IllegalArgumentException ex) {
            taskStartDay.setSelectedIndex(0);
        }

    }//GEN-LAST:event_taskStartMonthActionPerformed

    private void taskStartYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskStartYearActionPerformed
        // TODO add your handling code here:
        String month = (String) taskStartMonth.getSelectedItem();
        int year = Integer.parseInt((String) taskStartYear.getSelectedItem());
        if ("February".equals(month)) {
            taskStartDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, new GregorianCalendar().isLeapYear(year))));
        }

    }//GEN-LAST:event_taskStartYearActionPerformed

    private void taskEndMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskEndMonthActionPerformed
        // TODO add your handling code here:
        int fixedDay = taskEndDay.getSelectedIndex();
        String month = (String) taskEndMonth.getSelectedItem();
        int year = Integer.parseInt(taskEndYear.getSelectedItem().toString());
        boolean isLeap = ("February".equals(month) && new GregorianCalendar().isLeapYear(year)) ? true : false;
        taskEndDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, isLeap)));
        try {
            taskEndDay.setSelectedIndex(fixedDay);
        } catch (IllegalArgumentException ex) {
            taskEndDay.setSelectedIndex(0);
        }
    }//GEN-LAST:event_taskEndMonthActionPerformed

    private void taskEndYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskEndYearActionPerformed
        // TODO add your handling code here:
        String month = (String) taskEndMonth.getSelectedItem();
        int year = Integer.parseInt((String) taskEndYear.getSelectedItem());
        if ("February".equals(month)) {
            taskEndDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, new GregorianCalendar().isLeapYear(year))));
        }
    }//GEN-LAST:event_taskEndYearActionPerformed

    private void alertTimeMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alertTimeMonthActionPerformed
        // TODO add your handling code here:
        int fixedDay = alertTimeDay.getSelectedIndex();
        String month = (String) alertTimeMonth.getSelectedItem();
        int year = Integer.parseInt(alertTimeYear.getSelectedItem().toString());
        boolean isLeap = ("February".equals(month) && new GregorianCalendar().isLeapYear(year)) ? true : false;
        alertTimeDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, isLeap)));
        try {
            alertTimeDay.setSelectedIndex(fixedDay);
        } catch (IllegalArgumentException ex) {
            alertTimeDay.setSelectedIndex(0);
        }
    }//GEN-LAST:event_alertTimeMonthActionPerformed

    private void alertTimeYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alertTimeYearActionPerformed
        // TODO add your handling code here:]
        String month = (String) alertTimeMonth.getSelectedItem();
        int year = Integer.parseInt((String) alertTimeYear.getSelectedItem());
        if ("February".equals(month)) {
            alertTimeDay.setModel(new DefaultComboBoxModel(Helper.getDays(month, new GregorianCalendar().isLeapYear(year))));
        }
    }//GEN-LAST:event_alertTimeYearActionPerformed

    private void taskEndHourFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_taskEndHourFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(taskEndHour.getText(), true);
        taskEndHour.setValue(s);
    }//GEN-LAST:event_taskEndHourFocusLost

    private void alertTimeHourFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_alertTimeHourFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(alertTimeHour.getText(), true);
        alertTimeHour.setValue(s);
    }//GEN-LAST:event_alertTimeHourFocusLost

    private void taskStartMinuteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_taskStartMinuteFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(taskStartMinute.getText(), false);
        taskStartMinute.setValue(s);
    }//GEN-LAST:event_taskStartMinuteFocusLost

    private void taskEndMinuteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_taskEndMinuteFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(taskEndMinute.getText(), false);
        taskEndMinute.setValue(s);
    }//GEN-LAST:event_taskEndMinuteFocusLost

    private void alertTimeMinuteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_alertTimeMinuteFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(alertTimeMinute.getText(), false);
        alertTimeMinute.setValue(s);
    }//GEN-LAST:event_alertTimeMinuteFocusLost

    private void taskStartHourFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_taskStartHourFocusLost
        // TODO add your handling code here:
        String s = Helper.editNumericText(taskStartHour.getText(), true);
        taskStartHour.setValue(s);
    }//GEN-LAST:event_taskStartHourFocusLost

    private void taskNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taskNameKeyReleased
        // TODO add your handling code here:
        editedTask.setName(taskName.getText());
    }//GEN-LAST:event_taskNameKeyReleased

    private void taskDescriptionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taskDescriptionKeyReleased
        // TODO add your handling code here:
        editedTask.setDescription(taskDescription.getText());
    }//GEN-LAST:event_taskDescriptionKeyReleased

    private void taskContactsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taskContactsKeyReleased
        // TODO add your handling code here:
        editedTask.setContacts(taskContacts.getText());
    }//GEN-LAST:event_taskContactsKeyReleased

    private void taskStartDayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_taskStartDayMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_taskStartDayMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EditWindow dialog = new EditWindow(new javax.swing.JFrame(), true, isAdd);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });

    }

    public void recieveData(Task data) {
        if (data == null) {
            Calendar now = Calendar.getInstance();
            editedTask = new Task(UUID.randomUUID(), "New Task", "Description", now, now, "Some info", now, 0);
        } else {
            editedTask = new Task(data);
        }

        taskName.setText(editedTask.getName());
        taskDescription.setText(editedTask.getDescription());
        taskContacts.setText(editedTask.getContacts());
        Calendar c = editedTask.getStime();
        String hour = new Integer(c.get(Calendar.HOUR_OF_DAY)).toString();
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        taskStartHour.setValue(hour);
        String minute = new Integer(c.get(Calendar.MINUTE)).toString();
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        taskStartMinute.setValue(minute);
        int month = c.get(Calendar.MONTH);
        taskStartMonth.setSelectedIndex(month);
        Integer year = c.get(Calendar.YEAR);
        taskStartYear.setSelectedItem(year.toString());
        taskStartDay.setSelectedIndex(c.get(Calendar.DATE) - 1);

        c = editedTask.getFtime();
        hour = new Integer(c.get(Calendar.HOUR_OF_DAY)).toString();
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        minute = new Integer(c.get(Calendar.MINUTE)).toString();
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        taskEndHour.setValue(hour);
        taskEndMinute.setValue(minute);
        month = c.get(Calendar.MONTH);
        taskEndMonth.setSelectedIndex(month);
        year = c.get(Calendar.YEAR);
        taskEndYear.setSelectedItem(year.toString());
        taskEndDay.setSelectedIndex(c.get(Calendar.DATE) - 1);

        c = editedTask.getWtime();
        hour = new Integer(c.get(Calendar.HOUR_OF_DAY)).toString();
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        minute = new Integer(c.get(Calendar.MINUTE)).toString();
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        alertTimeHour.setValue(hour);
        alertTimeMinute.setValue(minute);
        month = c.get(Calendar.MONTH);
        alertTimeMonth.setSelectedIndex(month);
        year = c.get(Calendar.YEAR);
        alertTimeYear.setSelectedItem(year.toString());
        alertTimeDay.setSelectedIndex(c.get(Calendar.DATE) - 1);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox alertTimeDay;
    private javax.swing.JFormattedTextField alertTimeHour;
    private javax.swing.JFormattedTextField alertTimeMinute;
    private javax.swing.JComboBox alertTimeMonth;
    private javax.swing.JComboBox alertTimeYear;
    private javax.swing.JButton approveButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField taskContacts;
    private javax.swing.JTextArea taskDescription;
    private javax.swing.JComboBox taskEndDay;
    private javax.swing.JFormattedTextField taskEndHour;
    private javax.swing.JFormattedTextField taskEndMinute;
    private javax.swing.JComboBox taskEndMonth;
    private javax.swing.JComboBox taskEndYear;
    private javax.swing.JTextField taskName;
    private javax.swing.JComboBox taskStartDay;
    private javax.swing.JFormattedTextField taskStartHour;
    private javax.swing.JFormattedTextField taskStartMinute;
    private javax.swing.JComboBox taskStartMonth;
    private javax.swing.JComboBox taskStartYear;
    // End of variables declaration//GEN-END:variables
}
